<template>
    <el-table
            :data="partList"
            border
            rowKey="id"
            :summary-method="getSummaries"
            show-summary
            stripe
            style="width: 100%">
        <el-table-column
                type="index"
                prop="no"
                label="No"
        >
        </el-table-column>
        <el-table-column
                prop="partsNumber"
                :label="$t('part.partNo')"
                width="180">
        </el-table-column>
        <el-table-column
                prop="denomination"
                :label="$t('part.description')"
                width="320">
            <template slot-scope="part">
                <span>{{part.row.denomination}}/{{part.row.designation}}</span>
            </template>
        </el-table-column>
        <el-table-column
                v-if="urlAvailible('sale-price')"
                prop="salesPrice"
                :label="$t('part.salesPrice')"
        >
            <template slot-scope="scope">
                <span v-else>{{scope.row.salePrice}}</span>
            </template>
        </el-table-column>
        <el-table-column :label="$t('part.qty')" width="180">
            <template slot-scope="scope">
                <el-input-number
                        v-if="edit"
                        size="mini"
                        :min="min"
                        v-model="scope.row.number">
                </el-input-number>
                <span v-else>{{scope.row.number}}</span>
            </template>
        </el-table-column>
        <el-table-column :label="$t('part.oQty')"  prop="oQty" v-if="showOQty"></el-table-column>
        <el-table-column
                prop="systemName"
                :label="$t('part.system')"
                width="150">
        </el-table-column>
        <el-table-column :label="$t('operate')"  v-if="edit">
            <template slot-scope="scope">
                <div class="delete">
                    <i class="el-icon-delete" @click="deleteItem(scope.row.id)"> </i>
                </div>
            </template>
        </el-table-column>
    </el-table>
</template>

<script>
    import authMixins from '../../util/authMixins'
    export default {
        name: "sp-data-base",
        mixins:[authMixins],
        props:{
            partList:{
                type:Array,
                default:()=>[]
            },
            priceEdit:{
                type:Boolean,
                default:false
            },
            edit:{
                type:Boolean,
                default: true
            },
            showOQty:{
                type:Boolean,
                default:false
            }
        },
        data(){
            return {
                min: 0
            }
        },
        methods:{
            deleteItem(id){
                this.$emit('deleteItem',id)
            },
            getSummaries(params){
                const { columns, data } = params;
                let total = 0;
                let sums = Array(6);
                columns.forEach((column, index) => {
                    if (index === 0) {
                        sums[index] = 'Sum';

                    } else if(index !== 4) {
                        sums[index] = ''
                    } else {
                        data.forEach(item=>{
                            total += item.number
                        });
                        total += ' PC';
                        sums[index] = total
                    }
                });
                return sums;
            }
        }
    }
</script>

<style scoped>

</style>